﻿using System;
using System.Web.UI;
using Core.Library;
using DataObject;
using DataObject.Article;
using DataObject.Category;
using KMT.Controls.Common.Article;

namespace KMT.Controls.Article
{
    public partial class Detail : System.Web.UI.UserControl
    {
        protected int ArticleId;
        protected string Title = string.Empty;
        protected string Body = string.Empty;
        protected string Source = string.Empty;
        protected string CategoryName = string.Empty;
        protected int CategoryId = 0;
        protected string ArticleImage = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            ArticleId = Utils.GetIdFromRequestString(Utils.ArticleId, 0);

            if (ArticleId <= 0) return;
            ArticleDetail();

            var controlLeft = (ArticleLeft)LoadControl("~/Controls/Common/Article/ArticleLeft.ascx");
            controlLeft.CategoryId = CategoryId;
            ltrArticleLeft.Controls.Add(controlLeft);

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "hccs",
                                                        "<script>listComment(" + ArticleId + ", " +
                                                        (int)CategoryType.Article + ", " + SettingValues.PageIndex +
                                                        ");</script>");
        }

        public void ArticleDetail()
        {
            var o = ArticleController.Article_GetById(ArticleId);
            if (o == null) return;

            Title = o.Title;
            Body = o.Body;
            Source = o.Source;
            CategoryName = CategoryController.GetCategoryName(o.CategoryId);
            ArticleImage = string.Format("{0}/Thumb.ashx?Id={1}&tId={2}&Size=765", Constants.ThumbImage, o.ArticleId, (int)CategoryType.Article); ;
            CategoryId = o.CategoryId;

            var cHref = UrlHelpers.ListArticleByCategory(o.CategoryId, CategoryName);
            ltCategory.Text = string.Format("<a href=\"{0}\" class=\"black under\">{1}</a>", cHref, CategoryName);

            Page.Title = string.Format(".: {0} {1} :.", o.Title, Constants.SITE_TITLE);
        }
    }
}